package cn.ffcs.bpm.service;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import java.util.HashMap;
import java.util.List;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.base.service.BaseService;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.common.dao.AttachDAO;
import cn.ffcs.common.model.Attach;
import cn.ffcs.organ.model.Person;
import cn.ffcs.security.model.User;
import cn.ffcs.util.context.ApplicationContextUtil;
import cn.ffcs.bpm.dao.TransactorDAO;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpm.model.Transactor;
import cn.ffcs.bpm.model.ProcessConfig;

/**
 * 服务层<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class TransactorService<D extends Transactor> extends BaseService<D>
implements ITransactorService<D>
{
    private TransactorDAO transactorDAO;
    
    public void remove(Object entity, HashMap<String, Object> conds){
        transactorDAO.remove(entity, conds);
    }
    
    public void saveOrUpdate(List<D> transactors){
    	if(transactors!=null){
    		for(int i=0;i<transactors.size();i++){
    			super.saveOrUpdate(transactors.get(i));
    		}
    	}
    }
    
    public PageControl findByPage(Criterion cond,int currPage, int pageSize){
        if(cond!=null)
            cond = Restrictions.and(cond, combineCond());
        else
            cond=combineCond();
        return transactorDAO.findByPage(cond, currPage,pageSize);
    }

    public List findByPage(HashMap<String, Object> conds){
        return transactorDAO.findByPage(conds);
    }
    
    /**
     * 
     * 组合查询条件
     * @return
     */
    public Criterion combineCond(){
        User curUser=ApplicationContextUtil.getUserContextObject().getCurrUser();
        Person person = new Person();
        person.setId(new Integer(curUser.getPersonID()));
        Criterion cond = Restrictions.eq("userId",person);
        
        return cond;
    }
    
    
    
    /**
     * 
     * 组合查询条件
     * @return
     */
    public void saveTransactor(String nextPerson,String tranType,TransactProcessStatus transactProcessStatus){
        HashMap<String, Object> conds=new HashMap();
        conds.put("currProcess", transactProcessStatus.getId()+"");
        remove(null, conds);
        String[] ps= nextPerson.split(",");
        for(String pp:ps){
            Transactor transactor=new Transactor();
            transactor.setTranType(tranType);
            transactor.setCurrProcess(transactProcessStatus);
            Person person = new Person();
            person.setId(new Integer(pp));
            transactor.setUserId(person);
            save((D)transactor);
        }
        return;
    }

    
    public TransactorDAO getTransactorDAO()
    {
        return transactorDAO;
    }
    public void setTransactorDAO(TransactorDAO transactorDAO)
    {
        this.transactorDAO = transactorDAO;
    }
    
    
}
